class arr:
    def __init__(self):
        self.data = None
        self.next = None


class list:
    def __init__(self):
        self.head = arr()
        self.length = 0

    def insert(self, pos, data):
        assert pos >= 0 and pos <= self.length

        # 找位置
        temp = self.head
        while pos > 0:
            temp = temp.next
            pos -= 1

        newdata = arr()
        newdata.data = data
        newdata.next = temp.next
        temp.next = newdata
        self.length += 1

    def append(self, data):
        self.insert(self.length, data)

    def remove(self, pos):
        assert pos < self.length

        # 找位置
        temp = self.head
        while pos > 0:
            temp = temp.next
            pos -= 1

        if temp.next is not None:
            delarr = temp.next
            temp.next = delarr.next
            self.length -= 1

    def foreach(self):
        temp = self.head.next
        while temp is not None:
            yield temp.data
            temp = temp.next


if __name__ == '__main__':
    l = list()
    l.append(1)
    l.append(2)
    for x in l.foreach():
        print(x)
    l.insert(2, 3)
    for x in l.foreach():
        print(x)

